Ccay.Module.define("ccay.security.role.grantOperation2Role",function(page,$S){
    var treeBeforeCheck=true;
    var treeObject;
    //zTree setting
    var setting = {
				check : {
					enable : true,
					beforeCheck : false,
					chkboxType : {
						"Y" : "ps",
						"N" : "ps"
					}
				},
				data : {
					key : {
						name : "name",
						children : "children",
						title : "title",
						checked : "checked"
					}
				},
				callback : {
					beforeCheck : function() {
						return treeBeforeCheck;
					}
				}
			};
	page.init=function(role){
		page.initRole(role);
		page.findResourceList(role.id);
		
	};
	
	/**
	 * @description 初始化父元素
	 */
	page.initRole = function(role){
		$S("#roleId").val(role.id);
		$S("#roleName").text(role.name);
		if(role.description){
			$S("#roleDesc").text(role.description);
		}
	};
	
	/** 
	 * 获取权限列表 绘制树
	 */
	page.findResourceList = function(roleId){
		Ccay.doGet("services/manage/security/permissionManage/findPermissionTree",{roleId:roleId},function(resources){
			//左侧树初始化
			treeObject = Ccay.tree.init($("#permissionSelectTree"), setting, resources,false,null);
			treeObject.expandAll(true);
			//右侧列表初始化
			Role.initHasPermissionList(resources,'#hasPermissionsList');
		});
	};
	
   
   
   /**
    * 保存有权限资源点
    */
   page.saveRolePermissions = function(){
   		var checkedNodes = treeObject.getCheckedNodes();
   		var hasPermissions = [];
   		for(var i=0;i<checkedNodes.length;i++){
   			if(checkedNodes[i].permissionString){
   				hasPermissions.push(checkedNodes[i].permissionString);
   			}
   		}
   		Ccay.doPut({
 			async:false,
 			url:"services/manage/security/permissionManage/saveRolePermissions/"+$S("#roleId").val(),
 			data : hasPermissions,
 			success:function(data){
 				Ccay.doGet("services/manage/security/permissionManage/findPermissionTree",{roleId:$S("#roleId").val()},function(resources){
					//右侧列表刷新
					Role.initHasPermissionList(resources,'#hasPermissionsList');
					Ccay.UI.topBox($i18n('ccay.common.message.success'));
				});
 			}			
 		});
   }
});
